define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function data() {
                return {
                    item: {
                        user: {
                          
                        }
                    },
                    goodlist: [],
                    good_text: "点赞",
                    publish_text: "确认提交",
                    placeholder: "",
                    commentList: [],
                    pager: {},
                    name: "moduleTopic",
                    page: 1,
                    community_id:0,
                    mounted: 0,
                    user_info: {  tenement_type:[]},
                    event: {}
                };
            },
            el: "#pageTopic",
            created: function created() {
               if(this.$route.params.community_id){
                        this.community_id = this.$route.params.community_id;
                }
                this.fetchData();
                this.getUserInfo();
                app.wxJs();
            },
            
            mounted: function mounted() {
                if (this.mounted == 1) {
                    return;
                }
                //分页
                var _this = this;
                app.pager(function() {
                    if (_this.page > 1 && _this.lastpage != 1 && app.params.loadPage != 1) {
                        app.params.loadPage = 1;
                        _this.loadComment();
                    }
                });
                this.mounted = 1;
                $("#pageTopic").find(".page-content").scroll(function(event) {
                    var scrollTop = $(this).scrollTop();
                    var bannerH = 35;
                    if (scrollTop > bannerH) {
                        $("#pageTopic").find(".topic-head").addClass("user-nav-tab-fix");
                    } else {
                        $("#pageTopic").find(".topic-head").removeClass("user-nav-tab-fix");
                    }
                });
            },
            methods: {
                fetchData: function fetchData(reload) {
                    var _this = this;
                    this.placeholder = this.$route.params.placeholder;
                    app.get("sns/topic/detail", {
                        id: _this.$route.params.id
                    }, function(res) {
                        if (res.code != 200) {
                            app.alert(res.msg);
                            return;
                        }
                        var data = res.data;
                        if (data && data.id) {
                            _this.item = data;
                            _this.good_text = data.has_good ? "已赞" : "点赞";
                        }
                    });
                    this.loadComment(reload);
                    this.loadGoodList(reload);
                },
                getUserInfo:function(){
                    var that = this;
                    app.get("/api/user/info",{
                        fields:"user_id,store_id,phone,nickname,phone,avatarUrl,last_update_time,statistics.money,statistics.income,roleId,type_str,community,community.rooms_count,community.houses_count,community.bills_count,topic_count,waitingpay,tenement_type,wxopenid"
                    },function(res){
                         if(res.code==200){
                             that.user_info = res.data;
                         }
                        
                    });
               },
                loadGoodList: function loadGoodList() {
                    //评论列表
                    var _this = this;
                    app.get("sns/topic/thumbs", {
                        topic_id: _this.$route.params.id,
                        page: 1,
                        limit: 20,
                       
                    }, function(res) {
                        if (res.code != 200) {
                            app.alert(res.msg);
                            return;
                        }
                        _this.goodlist = res.data.list;
                    });
                },
                loadComment: function loadComment(reload) {

                    //评论列表
                    var _this = this;
                    if(reload==1){
                        _this.commentList = [];
                        _this.page = 1;
                        _this.lastpage =0;
                        _this.loadPage =0;
                    }
                    app.get("sns/comment/get", {
                        pid: _this.$route.params.id,
                        page: this.page,
                        limit: 20
                    }, function(res) {
                        if (res.code != 200) {
                            app.alert(res.msg);
                            return;
                        }
                        console.log(res);
                        if (res.data.list.length === 0) {
                            _this.lastpage = 1;
                        }
                        _this.commentList = _this.commentList.concat(res.data.list);
                        _this.page++;
                        app.params.loadPage = 0;
                    });
                },
                showAddComment: function showAddComment(act) {
                    var placeholder = "这里填写报价及描述...";
                    var comment_id = 0;
                    this.$router.push({
                        name: "addComment",
                        params: {
                            pid: this.$route.params.id,
                            comment_id: comment_id,
                            placeholder: placeholder,
                            type: act
                        }
                    });
                },
                delTopic: function delTopic() {
                    var pid = this.$route.params.id;
                    var _this = this;
                    app.confirm({
                        text: "确认删除任务吗？",
                        yes: function yes() {
                            app.get("sns/topic/del", {
                                id: pid
                            }, function(data) {
                                if (data.code == 200) {
                                    $("[topic-item='" + pid + "']").hide();
                                    app.alert("恭喜你，删除成功");
                                    _this.$router.go(-1);
                                } else {
                                    app.alert(data.msg);
                                }
                            });
                        }
                    });
                },
                share: function share(em) {
                    var obj = $(em.currentTarget);
                    var url = "http://" + window.location.host + "/#/topic/" + $(obj).attr("id");
                    app.showLoad();
                    var params = {
                        title: $(obj).attr("title"),
                        // 分享标题
                        link: url,
                        // 分享链接
                        imgUrl: $(obj).attr("imgUrl"),
                        // 分享图标
                        desc: $(obj).attr("desc") ? $(obj).attr("desc") : $(obj).attr("title"),
                        success: function success() {
                            // 用户确认分享后执行的回调函数
                            app.wxDefShare();
                        },
                        cancel: function cancel() {}
                    };
                    app.wxShare(params, function() {
                        $(".pageShow").append('<div id="shareDiv"  onclick="$(this).remove()"></div>');
                        app.hideLoad();
                    });
                },
                previewImage: function previewImage(_this) {
                    var obj = $(_this.currentTarget);
                    var imgs = $(obj).parent(".img-list").find("img");
                    var urls = [];
                    $(imgs).each(function(index, el) {
                        urls.push($(el).attr("src"));
                    });
                    app.wx(function(wx) {
                        wx.previewImage({
                            current: $(obj).find("src"),
                            urls: urls
                        });
                    });
                },
                add_goods: function add_goods(id) {
                    var indexVm = app.getVm("index");
                    var items = indexVm ? indexVm.items : "";
                    var vm = this;
                    var _this = this;
                    var item = vm.item;
                    //console.log(item);
                    var goodLists = this.goodlist;
                    //console.log(goodLists);
                    app.add_goods(id, function(data) {
                        if (data.error > 0) {
                            return;
                        }
                        var user = app.getUserInfo();
                        //当前页点赞数变化
                        if (data.data.type == "add") {
                            item.thumb_up_ed = 1;
                            item.good_count++;
                            //点赞头像减加一个
                            goodLists.push(user);
                        } else {
                            item.thumb_up_ed = 0;
                            item.good_count--;
                            //点赞头像减少一个
                            goodLists = $.grep(goodLists, function(one, v) {
                                if (one.user_id == user.user_id) {
                                    return false;
                                } else {
                                    return true;
                                }
                            });
                        }
                        //console.log(goodLists);
                        _this.goodlist = goodLists;
                    });
                },
                handle: function handle(em) {
                    var _this = this;
                    var obj = $(em.currentTarget);
                    var id = $(obj).attr("data-id");
                    var user_id = $(obj).attr("data-user-id");
                    var topic_user_id = this.item.user_id;
                    var pid = this.item.id;
                    var placeholder = "@" + $(obj).attr("data-nickname");
                    var params = [ {
                        text: "回应"
                    } ];
                    var user_info = app.getUserInfo();
                    if (user_info.user_id == topic_user_id || user_id == user_info.user_id) {
                        var param = [ {
                            text: "删除"
                        },{
                            text: "采纳"
                        } ];
                        params = params.concat(param);
                    }
                    app.handle(params);
                    //回应
                    $("#handle .handle-options-one").eq("0").click(function(event) {
                        _this.$router.push({
                            name: "addComment",
                            params: {
                                pid: pid,
                                comment_id: id,
                                placeholder: placeholder,
                                type: "general"
                            }
                        });
                    });
                     //删除
                    $("#handle .handle-options-one").eq("1").click(function(event) {
                        var comment_id = id;
                        app.hide_popup();
                        app.confirm({
                            text: "确认删除这条评论吗？",
                            yes: function yes() {
                                app.get("sns/comment/del", {
                                    id: comment_id
                                }, function(data) {
                                    if (data.code == 200) {
                                        $(".comment-list").find("[data-id='" + comment_id + "']").hide();
                                        _this.item.comment_count--;
                                        var indexVm = app.getVm("index");
                                        var items = indexVm ? indexVm.items : "";
                                        //首页列评论数减少1
                                        if (items) {
                                            items = $.map(items, function(one) {
                                                if (one.id == _this.item.id) {
                                                    one.comment_count--;
                                                }
                                                return one;
                                            });
                                        }
                                        app.alert("恭喜你，删除成功");
                                    } else {
                                        app.alert(data.msg);
                                    }
                                });
                            }
                        });
                    });
                    //采纳

                    $("#handle .handle-options-one").eq("2").click(function(event) {
                        var comment_id = id;
                        app.hide_popup();
                        app.confirm({
                            text: "确认采纳这条报价吗？",
                            yes: function yes() {
                                app.post("sns/comment/add", {
                                    id: comment_id,
                                    is_best:1
                                }, function(data) {
                                    if (data.code == 200) {
                                        _this.fetchData(1);
                                        app.alert("恭喜你，采纳成功！");
                                    } else {
                                        app.alert(data.msg);
                                    }
                                });
                            }
                        });
                    });
                }
            }
        }).$mount("#app");
    };
    module.exports = controller;
});
